﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ModernUILogViewer.ViewModel;
using System.Windows.Input;
using Microsoft.Practices.Unity;
using ModernUILogViewer.Business;
using System.Threading;
using System.Net;
using System.Text;
using System.Diagnostics;

namespace ModernUILogViewerUnitTest
{
    [TestClass]
    public class UnitTest1
    {        
        class DialogServiceTest : IDialogService
        {
            public string[] files;

            public string OpenLogFile()
            {
                return null;
            }

            public string[] OpenFile(bool multi)
            {
                return files;
            }

            public string SaveFile()
            {
                return null;
            }            

            
        }

        [TestMethod]
        public void GetProductVersion()
        {
            ServiceUpdater updater = new ServiceUpdater();

            // decomment these lines if you are under a proxy
            //WebProxy proxy = new WebProxy("", 0);
            //proxy.Credentials = new NetworkCredential("", "", "");
            //updater.Proxy = proxy;

            ModernUILogViewer.Business.ServiceUpdater.VersionInfo info = updater.CheckForUpdates("http://dl.dropboxusercontent.com/u/55285635/modernloganalyzer.xml");

            Assert.IsNotNull(info);
        }

        [TestMethod]
        public void TestProcessFiles()
        {
            MainViewModel mainVM = new MainViewModel();
            mainVM.OptionsVM.DateRegExp = @"^\w* \d{2} \d{2}:\d{2}:\d{2}";
            mainVM.OptionsVM.IncludeDateMatch = true;
            mainVM.OptionsVM.IncludeLevelMatch = true;
            mainVM.OptionsVM.LevelGroupMatch = 1;
            mainVM.OptionsVM.LineBeginningRegExp = @"^\w* \d{2} \d{2}:\d{2}:\d{2}";
            mainVM.OptionsVM.DateFormat = @"MMM dd HH:mm:ss";
            mainVM.OptionsVM.LevelRegExp = @"\[(INFO|DEBUG|ERROR|FATAL|WARN)\]:.*=>";
            mainVM.OptionsVM.SelectedCultureInfo = new System.Globalization.CultureInfo("en-US");

            HomeViewModel vm = new HomeViewModel(mainVM);

            string[] files = new string[] { "test_part 1.log", "test_part 2.log" };

            DispatcherUtil.DoEvents();

            int idx = 0;

            foreach (string file in files)
            {
                vm.ProcessFile(file, idx++);
            }

            Assert.IsTrue(vm.EntriesCount > 0);

        }

        [TestMethod]
        public void TestOpenFiles()
        {
            MainViewModel mainVM = new MainViewModel();
            mainVM.OptionsVM.DateRegExp = @"^\w* \d{2} \d{2}:\d{2}:\d{2}";
            mainVM.OptionsVM.IncludeDateMatch = true;
            mainVM.OptionsVM.IncludeLevelMatch = true;
            mainVM.OptionsVM.LevelGroupMatch = 1;
            mainVM.OptionsVM.LineBeginningRegExp = @"^\w* \d{2} \d{2}:\d{2}:\d{2}";
            mainVM.OptionsVM.DateFormat = @"MMM dd HH:mm:ss";
            mainVM.OptionsVM.LevelRegExp = @"\[(INFO|DEBUG|ERROR|FATAL|WARN)\]:.*=>";
            mainVM.OptionsVM.SelectedCultureInfo = new System.Globalization.CultureInfo("en-US");

            HomeViewModel vm = new HomeViewModel(mainVM);

            IUnityContainer container = UnityContainerProvider.Instance;

            container.RegisterType<IDialogService>(DialogServiceNS.DLG_OPEN_MULTIPLE_LOG_FILES, 
                new InjectionFactory(cont =>
                {
                    DialogServiceTest dserviceTest = new DialogServiceTest
                    {
                        files = new string[] { "test_part 1.log", "test_part 2.log" }
                    };

                    return dserviceTest;
                })
            );

            DispatcherUtil.DoEvents();            

            ICommand ic = vm.Open;

            ic.Execute(null);

            Thread.Sleep(4000);

            Assert.IsTrue(vm.EntriesCount > 0);
        }
    }
}
